package com.qian.service.ddj.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.qian.api.ddj.model.DaifuLog;
import com.qian.api.ddj.service.IDaifuService;
import com.qian.service.ddj.dao.DaifuRepository;
import com.qian.service.ddj.dao.UserRepository;
import com.qian.service.ddj.entity.DdjDaifuLog;
import com.qian.service.ddj.entity.DdjUser;
import com.yaowk.api.utils.HttpUtils;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.Map;

import static com.qian.service.ddj.Constant.*;

@Service
public class DaifuServiceImpl extends BaseServiceImpl<DaifuLog, DdjDaifuLog, Integer> implements IDaifuService {

    @Autowired
    private DaifuRepository dao;
    @Autowired
    private UserRepository userDao;
    @PersistenceContext
    private EntityManager em;

    @Transactional
    public DaifuLog send(DaifuLog daifuLog) {

        DdjUser user = userDao.findOne(daifuLog.getUserId());
        if (user.getBalance().compareTo(daifuLog.getMoney()) < 0) {
            daifuLog.setRespCode("-1");
            daifuLog.setRespInfo("余额不足");
            return super.save(daifuLog);
        }

        Map<String, String> map = new HashMap();
        map.put("accNo", daifuLog.getCardNo());
        map.put("accName", daifuLog.getName());
        map.put("transAmount", daifuLog.getMoney().toString());
        map.put("userid", USERID);
        /** 参与验签的字段 */
        String sign = String.format(DAIFU_TEMPLATE, daifuLog.getName(), daifuLog.getCardNo(), daifuLog.getMoney().toString(), USERID, KEY);
        sign = DigestUtils.md5DigestAsHex(sign.getBytes());
        /** 上送的值为 */
        map.put("sign", sign);
        map.put("orderCode", "mb_withdraw");
        String receive = HttpUtils.post(URL, HttpUtils.formatData(map));
        System.out.println(receive);
        Map<String, String> result = JSON.parseObject(receive, Map.class);
        if ("00".equals(result.get("respCode"))) {
            daifuLog.setOrderId(result.get("orderId"));
            daifuLog.setRespCode(result.get("respCode"));
            daifuLog.setRespInfo(result.get("respInfo"));
            Query query = em.createNativeQuery("update ddj_user set balance = balance - ?1 where id = ?2");
            query.setParameter(1, daifuLog.getMoney());
            query.setParameter(2, daifuLog.getUserId());
            int update = query.executeUpdate();
            if (update < 1) {
                throw new RuntimeException("余额更新错误");
            }
        } else {
            daifuLog.setRespCode(result.get("respCode"));
            daifuLog.setRespInfo("系统错误，请联系客服");
        }
        daifuLog = super.save(daifuLog);
        return daifuLog;
    }

    protected BaseRepository<DdjDaifuLog, Integer> getDao() {
        return dao;
    }

    protected DdjDaifuLog model2Entity(DaifuLog model) {
        DdjDaifuLog ddjDaifuLog = null;
        if (!ObjectUtils.isEmpty(model)) {
            ddjDaifuLog = new DdjDaifuLog();
            ddjDaifuLog.setId(model.getId());
            ddjDaifuLog.setCardNo(model.getCardNo());
            ddjDaifuLog.setMoney(model.getMoney());
            ddjDaifuLog.setName(model.getName());
            ddjDaifuLog.setOrderId(model.getOrderId());
            ddjDaifuLog.setRespCode(model.getRespCode());
            ddjDaifuLog.setRespInfo(model.getRespInfo());
            ddjDaifuLog.setUserId(model.getUserId());
        }
        return ddjDaifuLog;
    }

    protected DaifuLog entity2model(DdjDaifuLog entity) {
        DaifuLog daifuLog = null;
        if (!ObjectUtils.isEmpty(entity)) {
            daifuLog = new DaifuLog();
            daifuLog.setId(entity.getId());
            daifuLog.setCardNo(entity.getCardNo());
            daifuLog.setMoney(entity.getMoney());
            daifuLog.setName(entity.getName());
            daifuLog.setOrderId(entity.getOrderId());
            daifuLog.setRespCode(entity.getRespCode());
            daifuLog.setRespInfo(entity.getRespInfo());
            daifuLog.setUserId(entity.getUserId());
            daifuLog.setCreateTime(entity.getCreateTime());
        }
        return daifuLog;
    }
}
